package com.blogs.security.service.impl;

import com.blogs.domain.db.SysUser;
import com.blogs.security.dto.SecurityUser;
import com.blogs.service.user.SysUserService;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * Description
 *
 * @author hubiao
 * @since 2020-09-27 15:09
 */
@Service
public class CustomUserDetailsServiceImpl implements UserDetailsService {

    @Resource
    private SysUserService sysUserService;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        //Collection<GrantedAuthority> authorities = new ArrayList<>();
        // 从数据库中取出用户信息
        SysUser user = sysUserService.selectByName( username );

        // 判断用户是否存在
        if (user == null) {
            throw new UsernameNotFoundException( "用户名不存在" );
        }

        // TODO  添加权限

        // 返回UserDetails实现类
        return new SecurityUser( user );
    }
}
